package kz.pompei.ifacer.testers;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

import kz.pompei.ifacer.cursor_manager.CursorManager;
import kz.pompei.ifacer.cursor_manager.def.DefCursorManager;
import kz.pompei.ifacer.model.CursorInfo;
import kz.pompei.ifacer.model.CursorKind;

public class DefCursorManagerTester extends JPanel {
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setTitle("Тестирование курсоров");
    frame.setBounds(100, 100, 1600, 600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    final DefCursorManagerTester pane = new DefCursorManagerTester(new DefCursorManager());
    frame.setContentPane(pane);
    
    frame.setVisible(true);
  }
  
  private final List<Point> points = new ArrayList<>();
  
  class Area {
    int x, y, width, height;
    CursorInfo cursorInfo;
    
    public Area(int x, int y, int width, int height, CursorInfo cursorInfo) {
      this.x = x;
      this.y = y;
      this.width = width;
      this.height = height;
      this.cursorInfo = cursorInfo;
    }
    
    public void paint(Graphics g) {
      g.setColor(Color.BLACK);
      g.drawRect(x, y, width, height);
      int H = g.getFontMetrics().getHeight();
      g.drawString(cursorInfo == null ? "NULL" :cursorInfo.display(), x + 5, y + H);
    }
    
    public boolean contains(Point point) {
      return new Rectangle(x, y, width + 1, height + 1).contains(point);
    }
  }
  
  private final List<Area> areas = new ArrayList<>();
  private final CursorManager cursorManager;
  
  public DefCursorManagerTester(CursorManager cursorManager) {
    this.cursorManager = cursorManager;
    
    {
      List<CursorInfo> ciList = new ArrayList<>();
      for (CursorKind ck : CursorKind.values()) {
        ciList.add(new CursorInfo(ck, null));
      }
      for (CursorKind ck : CursorKind.values()) {
        ciList.add(new CursorInfo(ck, "EDIT"));
      }
      ciList.add(null);
      ciList.add(new CursorInfo(CursorKind.MOVE, "fromRIGHT"));
      
      int left = 10, top = 10, width = 200, height = 50, dist = 20, count = 5;
      int col = 0, row = 0;
      for (CursorInfo ci : ciList) {
        int x = left + col * (width + dist);
        int y = top + row * (height + dist);
        areas.add(new Area(x, y, width, height, ci));
        
        col++;
        if (col >= count) {
          col = 0;
          row++;
        }
      }
      
    }
    
    addMouseMotionListener(new MouseMotionListener() {
      @Override
      public void mouseMoved(MouseEvent e) {
        Point point = e.getPoint();
        
        resetCursor(point);
      }
      
      @Override
      public void mouseDragged(MouseEvent e) {}
    });
    
    addMouseListener(new MouseListener() {
      @Override
      public void mouseReleased(MouseEvent e) {}
      
      @Override
      public void mousePressed(MouseEvent e) {
        points.add(e.getPoint());
        repaint(100);
      }
      
      @Override
      public void mouseExited(MouseEvent e) {}
      
      @Override
      public void mouseEntered(MouseEvent e) {}
      
      @Override
      public void mouseClicked(MouseEvent e) {}
    });
  }
  
  private void resetCursor(Point point) {
    for (Area area : areas) {
      if (area.contains(point)) {
        setCursor(cursorManager.getCursor(area.cursorInfo));
        return;
      }
    }
    
    setCursor(null);
  }
  
  @Override
  public void paint(Graphics g) {
    for (Area area : areas) {
      area.paint(g);
    }
    g.setColor(Color.RED);
    for (Point point : points) {
      g.drawLine(point.x, point.y, point.x, point.y);
    }
  }
}
